package cn.com.xf.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.common.StringUtil;
import cn.com.xf.domain.ReportSummary;
import cn.com.xf.domain.TblCheckReport;
import cn.com.xf.service.CompanyService;
import cn.com.xf.service.ReportService;
import cn.com.xf.utils.DateUtils;
import cn.com.xf.web.PageModel;
import cn.com.xf.web.form.ReportForm;

@Controller
@RequestMapping("/check")
public class ReportController extends AbstractBaseController {

	@Autowired
	private ReportService reportService;
	@Autowired
	private CompanyService companyService;
	
	@RequestMapping("/reportList")
	public ModelAndView reportList(HttpServletRequest request, HttpServletResponse response, ReportForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/report_list", Constants.FORM_COMMAND_KEY, form);
		String siteid = StringUtil.StringFilter(form.getSiteid());
		String userid = StringUtil.StringFilter(form.getUserid());
		String deptid = StringUtil.StringFilter(form.getDeptid());
		String startdateString = StringUtil.StringFilter(form.getStartdate());
		String enddateString = StringUtil.StringFilter(form.getEnddate());
		if (siteid.equals("") || !isSiteId(siteid)) {
			siteid = getDefaultSiteid();
		}
		form.setSiteid(siteid);
		form.setUserid(userid);
		form.setDeptid(deptid);
		form.setStartdate(startdateString);
		form.setEnddate(enddateString);
		Date startdate = null, enddate = null;
		if (!startdateString.equals("")) {
			startdate = DateUtils.parseDate(startdateString);
		}
		if (!enddateString.equals("")) {
			enddate = DateUtils.parseDate(enddateString);
		}		
		int count = reportService.selectReportCount(userid, siteid, deptid, startdate, enddate);
		if (count == 0)
			return mv;

		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();
		List<TblCheckReport> list = reportService.selectReport(begin, pageSize, userid, siteid, deptid, startdate, enddate);
		Map<String, Object> map = companyService.getDetps();
		for (TblCheckReport report: list) {
			if (report.getDeptid() == null) {
				report.setDeptname("");
			} else {
				report.setDeptname(map.get(report.getDeptid())+"");
			}
		}
		mv.addObject("list", list);
		return mv;
	}
	
	@RequestMapping("/totalReportList")
	public ModelAndView totalReportList(HttpServletRequest request, HttpServletResponse response, ReportForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/report_list", Constants.FORM_COMMAND_KEY, form);
		String siteid = StringUtil.StringFilter(form.getSiteid());
		String userid = StringUtil.StringFilter(form.getUserid());
		String deptid = StringUtil.StringFilter(form.getDeptid());
		String startdate = StringUtil.StringFilter(form.getStartdate());
		String enddate = StringUtil.StringFilter(form.getEnddate());
		if (siteid.equals("") || !isSiteId(siteid)) {
			siteid = getDefaultSiteid();
		}
		form.setSiteid(siteid);
		form.setUserid(userid);
		form.setDeptid(deptid);
		form.setStartdate(startdate);
		form.setEnddate(enddate);
		int count = reportService.selectAllReportCount(userid, siteid, deptid, startdate, enddate);
		if (count == 0)
			return mv;

		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();
		List<TblCheckReport> list = reportService.selectAllReport(begin, pageSize, userid, siteid, deptid, startdate, enddate);
		mv.addObject("list", list);
		return mv;
	}
	
	@RequestMapping("/summaryList")
	public ModelAndView summaryList(HttpServletRequest request, HttpServletResponse response, ReportForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/summary_report_list", Constants.FORM_COMMAND_KEY, form);
		String siteid = StringUtil.StringFilter(form.getSiteid());
		String userid = StringUtil.StringFilter(form.getUserid());
		String deptid = StringUtil.StringFilter(form.getDeptid());
		String monthString = StringUtil.StringFilter(form.getMonth());
		if (siteid.equals("") || !isSiteId(siteid)) {
			siteid = getDefaultSiteid();
		}
		form.setSiteid(siteid);
		form.setUserid(userid);
		form.setDeptid(deptid);
		form.setMonth(monthString);
		Date month = null;
		if (!monthString.equals("")) {
			month = DateUtils.parseMonth(monthString);
		}
		List<ReportSummary> list = reportService.getSummary(userid, siteid, deptid, month);
		mv.addObject("list", list);
		Map<String, Object> map = companyService.getDetps();
		for (ReportSummary summary: list) {
			if (summary.getDeptid() == null) {
				summary.setDeptname("");
			} else {
				summary.setDeptname(map.get(summary.getDeptid())+"");
			}
		}
		mv.addObject("list", list);
		return mv;
	}
}
